****************************************************************************************
****************************************************************************************
** TABLE A4
****************************************************************************************
****************************************************************************************
use "${path}data_table.dta", clear

* run one time 
preserve 
egen ididlabex=group(labexid id_2019)
egen ididy =group(idid  year)
tsset ididl year
gsort ididl year
gen npubs_m=  (npubs + l.npubs +  ll.npubs)/3
gen tcs_t_m=  (tcs_t + l.tcs_t +  ll.tcs_t)/3
gen citations_new_m=  (citations_new + l.citations_new + ll.citations_new)/3
gen nb_collab_same_labex_m=  (nb_collab_same_labex + l.nb_collab_same_labex +  ll.nb_collab_same_labex)/3
keep if year==year_labex & nb_labex_corrected==1
*drop cem_strata* cem_weights* cem_matched*
cem note_totale (#0) npubs_m citations_new_m chimie recherche_medicale physique sciences_ingenieur mathematiques SHS age , treatment(accepted)
ta cem_matched
keep id_2019 labexid cem* 
save "${path}cempper.dta", replace
su cem_matched* cem_weights*
restore
*drop  _m

merge m:1 id_2019 labexid using "${path}cempper.dta"
drop if _m==2
drop  _m

* run any nbr of times
preserve
label var nb_collab_same_labex "\emph{Links}"
label var citations_new "\emph{Cites}"
gen age_2010 = age if year ==2010
bys id_20:  egen age_in_2010=max( age_2010)
drop age_2010
gen discipline=0
replace discipline=1 if biologie_fondamentale ==1
replace discipline=2 if recherche_medicale ==1
replace discipline=3 if biologie_appliquee_ecologie ==1
replace discipline=4 if chimie ==1
replace discipline=5 if physique ==1
replace discipline=6 if sciences_univers ==1
replace discipline=7 if sciences_ingenieur ==1
replace discipline=8 if mathematiques ==1
replace discipline=9 if SS ==1
replace discipline=10 if SH ==1
local regnom    "orga_note_alt_rob"
qui eststo:  reghdfe nb_collab_same_labex treat2  if  id_year==1  &  criterion_grade==1 [aweight=cem_weights],   a( i.discipline#i.year age_in_2010#i.year female_new#i.year  year id_2019) cluster(labexid) 
qui :  estadd ysumm  
qui :  estadd scalar clusters = e(N_clust)
qui eststo:  reghdfe nb_collab_same_labex treat2  if   criterion_grade_alt==1 ,  a( i.discipline#i.year age_in_2010#i.year female_new#i.year  year id_2019) cluster(labexid) 
qui :  estadd ysumm 
qui :  estadd scalar clusters = e(N_clust)
qui eststo:  reghdfe nb_collab_same_labex treat2  if    criterion_note3==1 ,  a( i.discipline#i.year age_in_2010#i.year female_new#i.year  year id_2019) cluster(labexid) 
qui :  estadd ysumm  
qui :  estadd scalar clusters = e(N_clust)
esttab using  `regnom',   b(%4.3f) se(%4.3f) ar2 starlevels({$^{*}$} 0.1 {$^{**}$} 0.05 {$^{***}$} 0.01)  tex  label replace keep(treat2) stats(N clusters ymean r2_a, fmt(%9.0g %9.2g %9.2g  ) labels("Observations" "Number of Clusters" "Mean dep variable" "Adj. R-Square" )) nolegend nonotes
esttab,  compress ar2 starlevels(* 0.1 ** 0.05 *** 0.01)  b(%4.3f) se(%4.3f) label   keep(treat2) stats(N clusters ymean r2_a, fmt(%9.0g %9.2g %9.2g  ) labels("Observations" "Number of Clusters" "Mean dep variable" "Adj. R-Square" )) nonotes
eststo clear   
restore

 
* STAGGERED SPECIFICATIONS 
preserve
gen discipline=0
replace discipline=1 if biologie_fondamentale ==1
replace discipline=2 if recherche_medicale ==1
replace discipline=3 if biologie_appliquee_ecologie ==1
replace discipline=4 if chimie ==1
replace discipline=5 if physique ==1
replace discipline=6 if sciences_univers ==1
replace discipline=7 if sciences_ingenieur ==1
replace discipline=8 if mathematiques ==1
replace discipline=9 if SS ==1
replace discipline=10 if SH ==1
gen treated = 0
replace treated = year_labex +1  if year_labex !=. & accepted ==1
egen idlabex=group(labexid)
egen ididlabex=group(labexid id_2019)
eststo clear 
label var nb_collab_same_labex "\emph{Links}"
gen age_2010 = age if year ==2010
bys id_20:  egen age_in_2010=max( age_2010)
drop age_2010
keep if criterion_grade==1
csdid nb_collab_same_labex i.discipline#i.year age_in_2010#i.year female_new#i.year  year, method(reg) time(year) ivar(ididlabex) gvar(treated) cluster(idlabex) agg(event) saverif(even_x) wboot replace rseed(1)
estat group, estore(regx)
estat group, estsave(regx) replace
restore

preserve
use even_x, clear
csdid_stats event, wboot rseed(1)
restore
* COEFICIENTS SHOULD BE ADDED MANUALLY (COL 4)
